package pl.edu.agh.ros.proxy;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ChatManager;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;

public class ProxyUtils {

	private XMPPConnection connection;

	public ProxyUtils() {
		// TODO Auto-generated constructor stub
	}

	public Connection connect(String server, int port) throws XMPPException {
		ConnectionConfiguration config = new ConnectionConfiguration(server,
				port);
		connection = new XMPPConnection(config);
		connection.connect();
		return connection;
	}

	public void login(String user, String password) throws XMPPException {
		connection.login(user, password, String.valueOf(System
				.currentTimeMillis()));
	}

	public Chat createChat(String toUser) {
		ChatManager chatmanager = connection.getChatManager();
		Chat newChat = chatmanager.createChat(toUser, new MessageListener() {
			public void processMessage(Chat chat, Message message) {
				System.out.println("Received message: " + message);
			}
		});
		try {
			newChat.sendMessage("Howdy!");
		} catch (XMPPException e) {
			System.out.println("Error Delivering block");
		}
		return newChat;
	}

	public void disconnect() {
		connection.disconnect();
	}

}
